<?php

namespace App\Http\Controllers\Domain;

use App\Http\Controllers\Controller;
use App\Models\Game\item_template;
use App\Models\Goods;
use App\Models\Player;
use Illuminate\Http\Request;
use App\Models\Server;
use App\Helper;
use App\Models\Account;
use App\Models\Character;
use App\Http\Requests\PlayerChangePwdRequest;
use Pay;
use App\Events\PlayerMoneyChangeEvent;


class MallController extends Controller
{
    public $prefix='domain';

    public function index($domain,Request $request){
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_world',$server,'acore_world');
        $list=$server->goods;

        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $acc_id=json_decode( session('player'))->id;
        $characters=Character::where('account',$acc_id)->orderBy('logout_time','desc')->get();

        // 余额
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
        $money = Account::find(session('player')->id)->money;

        return view($this->prefix.'.user.mall',compact('server','list','characters','money'));
    }
    public function getData($domain){
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_world',$server,'acore_world');
        $list=$server->goods;
        $ids=[];
        foreach ($list as $item){
            array_push($ids,$item->item_id);
        }
        $templatesArr=[];
        $templates=item_template::whereIn('entry',$ids)->get(['entry','name','description','class','displayid','Quality','InventoryType','bonding',
            'ItemLevel','RequiredLevel','MaxDurability','dmg_type1','dmg_type2','dmg_min1','dmg_max1','dmg_min2','dmg_max2','delay','armor','block',
            'stat_type1','stat_value1', 'stat_type2','stat_value2', 'stat_type3','stat_value3', 'stat_type4','stat_value4',
            'stat_type5','stat_value5', 'stat_type6','stat_value6', 'stat_type7','stat_value7', 'stat_type8','stat_value8',
            'stat_type9','stat_value9', 'stat_type10','stat_value10', 'StatsCount',
            'holy_res','fire_res','nature_res','frost_res','shadow_res','arcane_res',
            'duration','flagsCustom']);
        foreach ($templates as $template){
            $templatesArr[$template->entry]=$template;
        }
        foreach ($list as $item){
//            $item->item;
            if (key_exists($item->item_id,$templatesArr)){
                $item->entry=$templatesArr[$item->item_id];
            }
            $item->count='';
        }
        $cats=$server->goodsCategory;

        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $acc_id=json_decode( session('player'))->id;
        $characters=Character::where('account',$acc_id)->orderBy('logout_time','desc')->get();

        // 余额
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
        $money = Account::find(session('player')->id)->money;

        return [
            'err'=>0,
            'data'=>compact('list','characters','cats')
        ];
    }



    public function buy($domain ,Request $request){
//        $data=$request->all();
        $goodsId=$request->get('goodsId');
        $count=$request->get('count');
        $guid=$request->get('guid');
//        dd($data);
        if (!$count){
//            return back()->with('msg','请输入购买数量');
            return ['err'=>-1,'msg'=>'请输入购买数量'];
        }
        if (!$guid) {
//            return back()->with('msg','请选择角色');
            return ['err'=>-1,'msg'=>'请选择角色'];
        }

        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');

        $goods=Goods::find($goodsId);
        $price = $goods->price * $count;
        if ($goods && $goods->server_id===$server->id){
            $char=Character::where('guid',$guid)->first();
            $acc=session('player');
            $acc_id=$acc->id;
            $username=$acc->username;
            if ($char && $char->account==$acc_id){
                Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
                $player = Player::getPlayer($server,$username);
                if ($price > $player->money){
//                    return back()->with('msg','抱歉，您的余额不足');
                    return ['err'=>-1,'msg'=>'您的积分不足'];
                }else{
                    $res='';
                    $err='';
//                    $server->SendItemToPlay($char->name,'商城购物','感谢支持!',$goods->item_id,$count,$res,$err);
                    $char->SendItem($server,'商城购物','感谢支持!',$goods,$count,$res,$err);
                    if ($err) {
//                        return back()->with('msg',$err);
                        return ['err'=>-1,'msg'=>$err];
                    }else{
                        $player->money -= $price;
                        $player->save();
                        // $user = User::find($server->user_id);
                        // $user->remain_money += $price;
                        // $user->save();
                        // event(new ManagerMoneyChangeEvent($server->user_id,$server->id,$acc_id,5,1,$price,0,$user->remain_money,'玩家'.$player->username.'使用'.$price.'积分购买道具'));
                        event(new PlayerMoneyChangeEvent($server->user_id,$server->id,$acc_id,2,2,$price,0,$player->money,'使用'.$price.'积分购买道具',$goodsId));
//                        return back()->with('msg','购买成功:'.$res);
                        return ['err'=>0,'msg'=>'购买成功：'.$res,'data'=>$player->money];
                    }
                }
            }else{
//                return back()->with('msg','非法请求');
                return ['err'=>-1,'msg'=>'非法请求'];
            }
        }else{
//            return back()->with('msg','没有此商品');
            return ['err'=>-1,'msg'=>'没有此商品'];
        }
    }
}
